using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Events;
using System;
using System.IO;

namespace housingsafety.server
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        /// <summary>
        /// ��������������
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public static IHostBuilder CreateHostBuilder(string[] args)
        {
            var builder = Host.CreateDefaultBuilder(args);

            var configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
                    .AddJsonFile("appsettings.json", optional: true).Build();
            var logEnable = configuration.GetSection("logEnable");
            if ("true".Equals(logEnable.Value, StringComparison.OrdinalIgnoreCase))
            {
                builder.UseSerilog((context, configuration) =>
                {
                    configuration
                        .MinimumLevel.Warning()
                        // ��־�����������ռ������ Microsoft ��ͷ��������־�����С����Ϊ Information
                        .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
                        .Enrich.FromLogContext()
                        // ������־���������̨
                        .WriteTo.Console()
                        // ������־������ļ����ļ��������ǰ��Ŀ�� logs Ŀ¼��
                        // �ռǵ���������Ϊÿ��
                        .WriteTo.File(Path.Combine(AppContext.BaseDirectory, "logs", @"runlog.txt"), rollingInterval: RollingInterval.Day);
                });
            }

            builder.ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.ConfigureKestrel((context, options) =>
                    {
                        //����Ӧ�÷�����Kestrel���������Ϊ50MB
                        options.Limits.MaxRequestBodySize = 150_428_800;
                    });

                    var configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
                    .AddJsonFile("hosting.json", optional: true).Build();
                    webBuilder.UseConfiguration(configuration).UseStartup<Startup>();
                });

            return builder;
        }
    }
}
